package Classic150;

public class 移除元素 {
    public int removeElement(int[] nums, int val) {
        /*
            考虑返回元素顺序，顺序双指针
         */
        /*int left = 0, right = 0;
        while (right < nums.length) {
            if (nums[right] != val) {
                nums[left++] = nums[right++];
            } else {
                right++;
            }
        }
        return left;*/

        /*
            不考虑返回元素顺序，首尾双指针优化
         */
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            if (nums[left] == val) {
                nums[left] = nums[right--];
            } else {
                left++;
            }
        }
        return left;
    }
}
